/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.rtx.base.impl;

import com.alee.laf.WebLookAndFeel;
import com.google.common.collect.Maps;
import im.rtx.base.service.ThemeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;

/**
 * Project  : rtx
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2014/9/7
 * License  : Apache License 2.0
 */
@Service
public class ThemeServiceImpl extends BaseLifeCycleImpl implements ThemeService {
    private static final Logger LOG = LoggerFactory.getLogger(ThemeServiceImpl.class);
    private Map<String, ImageIcon> imageIconCache = Maps.newHashMap();
    private Map<String, Painter> painterCache = Maps.newHashMap();
    @Override
    public ImageIcon getImageIcon(String namespace) {
        InputStream in = getClass().getResourceAsStream("/" + namespace);
        if(in != null){
            try {
                BufferedImage im = ImageIO.read(in);
                return new ImageIcon(im);
            }catch (IOException e){
                LOG.error("read imageIcon error", e);
            }
        }
        return null;
    }

    @Override
    public Painter getPainter(String namespace) {
        return null;
    }

    @Override
    public Color getColor(String namespace) {
        return null;
    }

    @PostConstruct
    private void setupLAF() {

        try {
            Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/font/wqy-microhei.ttc"));
            setDefaultFont(font.deriveFont(12f));
        } catch (Exception e) {
            LOG.error("load font error", e);
        }
    }



    public void setDefaultFont(Font vFont) {
        UIManager.put("ToolTip.font", vFont);
        UIManager.put("Table.font", vFont);
        UIManager.put("TableHeader.font", vFont);
        UIManager.put("TextField.font", vFont);
        UIManager.put("ComboBox.font", vFont);
        UIManager.put("TextField.font", vFont);
        UIManager.put("PasswordField.font", vFont);
        UIManager.put("TextArea.font", vFont);
        UIManager.put("TextPane.font", vFont);
        UIManager.put("EditorPane.font", vFont);
        UIManager.put("FormattedTextField.font", vFont);
        UIManager.put("Button.font", vFont);
        UIManager.put("CheckBox.font", vFont);
        UIManager.put("RadioButton.font", vFont);
        UIManager.put("ToggleButton.font", vFont);
        UIManager.put("ProgressBar.font", vFont);
        UIManager.put("DesktopIcon.font", vFont);
        UIManager.put("TitledBorder.font", vFont);
        UIManager.put("Label.font", vFont);
        UIManager.put("List.font", vFont);
        UIManager.put("TabbedPane.font", vFont);
        UIManager.put("MenuBar.font", vFont);
        UIManager.put("Menu.font", vFont);
        UIManager.put("MenuItem.font", vFont);
        UIManager.put("PopupMenu.font", vFont);
        UIManager.put("CheckBoxMenuItem.font", vFont);
        UIManager.put("RadioButtonMenuItem.font", vFont);
        UIManager.put("Spinner.font", vFont);
        UIManager.put("Tree.font", vFont);
        UIManager.put("ToolBar.font", vFont);
        UIManager.put("OptionPane.messageFont", vFont);
        UIManager.put("OptionPane.buttonFont", vFont);

        UIManager.put("Tree.paintLines", false); // 去掉Tree中的水平线
        UIManager.put("Tree.leftChildIndent", 0); // 去掉节点 缩进

        //UIManager.put("Tree.arrowDown", getIconByKey("main/arrowDown")); // 去掉Tree中的水平线
        //UIManager.put("Tree.arrowLeft", getIconByKey("main/arrowLeft")); // 去掉节点 缩进

        WebLookAndFeel.globalControlFont = vFont;
        WebLookAndFeel.globalTextFont = vFont;
        WebLookAndFeel.globalTitleFont = vFont;
        WebLookAndFeel.globalTooltipFont = vFont;
        WebLookAndFeel.globalAcceleratorFont = vFont;
        WebLookAndFeel.menuItemAcceleratorFont = vFont;
        WebLookAndFeel.globalAlertFont = vFont;
        WebLookAndFeel.globalMenuFont = vFont;

        // 配置weblaf
        WebLookAndFeel.setDecorateAllWindows(false);
        WebLookAndFeel.install();
        Locale.setDefault(Locale.CHINA);
    }
}
